package com.sdkx.app.view;

import java.util.ArrayList;
import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Camera.Area;

import com.sdkx.app.R;
import com.sdkx.app.util.Util.LocationPoint;

import cn.creable.gridgis.controls.App;
import cn.creable.gridgis.controls.ICustomDraw;
import cn.creable.gridgis.controls.ICustomDrawDataCenter;
import cn.creable.gridgis.controls.MapControl;
import cn.creable.gridgis.display.Display;
import cn.creable.gridgis.geodatabase.Feature;
import cn.creable.gridgis.geodatabase.FeatureClass;
import cn.creable.gridgis.geodatabase.IFeature;
import cn.creable.gridgis.geometry.GeometryType;
import cn.creable.gridgis.geometry.IEnvelope;
import cn.creable.gridgis.geometry.IGeometry;
import cn.creable.gridgis.geometry.LineString;
import cn.creable.gridgis.geometry.Point;
import cn.creable.gridgis.util.Image;
import cn.creable.ucmap.OpenSourceMapLayer.POI;

public class MyCustomDraw implements ICustomDraw, ICustomDrawDataCenter {

    private MapControl mapControl;
    private Point[] pts;
    private Image img;
    private FeatureClass fc;
    private String[] text;
    private Activity act;
    private Vector<POI> poiList = new Vector<POI>();
    private Vector<Area> areaList = new Vector<Area>();
    private Paint paint;
    private Point pt = new Point();

    private Bitmap gpsImage;//gps点图片符号

    private boolean needRefresh=false;//是否需要刷新地图

    public MyCustomDraw(MapControl mapControl, Point[] pts, String[] text, Activity act) {
        this.mapControl = mapControl;
        this.pts = pts;
        this.text = text;
        // FeatureClass构造初始化方法传递的参数
        fc = new FeatureClass(new String[]{"text"}, GeometryType.Point);
        // IFeature地图要素
        Vector<IFeature> fts = new Vector<IFeature>();
        for (int i = 0; i < pts.length; ++i) {
            // IGeometry几何类型接口
            // 地图要素，是地图上的所有形状地图要素的抽象。
            IFeature ft = new Feature(pts[i], new String[]{text[i]});
            fts.addElement(ft);
        }
        // 设置全部地图要素
        fc.setFeatures(fts);
        // 读取图片
        BitmapDrawable bmpDraw = (BitmapDrawable) App.getInstance().getResources().getDrawable(R.drawable.gps);
        img = new Image(bmpDraw.getBitmap());
        this.act = act;
        paint = new Paint();
        paint.setTextSize(18);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    }

    public Vector<POI> getPOIList() {
        return poiList;
    }

    public void addPOI(POI poi) {
        this.poiList.addElement(poi);
    }

    public void addArea(Area area) {
        this.areaList.addElement(area);
    }

    @Override
    public void draw(Canvas g) {
        if (pts == null)
            return;
        int length = pts.length;
        // Display绘图对象，负责绘制地图。
        Display d = (Display) mapControl.getDisplay();
        Canvas oldG = d.getCanvas();
        d.setCanvas(g);
        for (int i = 0; i < length; ++i) {
            d.DrawImagePoint(pts[i], img);
        }
        d.setCanvas(oldG);
        //d.setLabelColor(0xFF000000);
        d.setLabelSize(30);
        d.setLabelColor(Color.RED);
        d.labelFeatureClass(g, fc, 0, img.getWidth(), img.getWidth() / 4);
    }

    @Override
    public IGeometry getGeometry(int index) {
        return pts[index];
    }

    @Override
    public int getGeometryNum() {
        return pts.length;
    }

    @Override
    public void onGeometrySelected(int index, IGeometry geo) {
        //自定义点的点击响应事件
        Builder builder1 = new AlertDialog.Builder(act);
        builder1.setTitle("自定义信息");
        builder1.setMessage(text[index]);
        builder1.setCancelable(true);
        builder1.setPositiveButton("OK", null);
        builder1.create().show();
    }

    /**
     * 设置轨迹
     *
     * @param locPoints gps点数组，也就是轨迹
     */
    private LineString historyLine;//轨迹线
    private LocationPoint curLoc;//当前gps点

    public void setLocaitonPoints(ArrayList<LocationPoint> locPoints) {
        if (locPoints == null)
            historyLine = null;
        else {
            //根据LocationPoint数组构建一个LineString对象
            int count = locPoints.size();
            if (count < 2) return;
            double[] points = new double[count * 2];
            for (int i = 0; i < count; ++i) {
                points[i * 2] = locPoints.get(i).lon;
                points[i * 2 + 1] = locPoints.get(i).lat;
            }
            historyLine = new LineString(points);
        }
    }

    /**
     * 设置当前gps点
     *
     * @param curLoc 当前gps点
     */
    public void setCurrentLocation(LocationPoint curLoc) {
        this.curLoc = curLoc;
        if (curLoc == null) return;
        if (mapControl.pointerStatus == 2 || mapControl.getRefreshManager().isThreadRunning() == true)
            return;//当mapControl有地图工具处于拖动模式，或者刷新线程没有执行完毕时，直接return
        IEnvelope env = mapControl.getExtent();
        //如果gps处于当前显示的地图范围内，则立即画出gps点
        if (env.getXMin() <= curLoc.lon && curLoc.lon <= env.getXMax() &&
                env.getYMin() <= curLoc.lat && curLoc.lat <= env.getYMax()) {
//            if (needRefresh) {//如果需要刷新则调用refresh
//                mapControl.refresh();
//                needRefresh = false;
//            } else {//如果不需要刷新则调用repaint，repaint的速度比refresh快的多，因为repaint不需要重新绘制地图，仅仅是将新的gps点绘制一下而已
//                mapControl.repaint();
//            }
        }
    }

}
